//*********************************************************************
//		Copyright (c) 2003 - 2008 Microsoft Corporation.
//      This code is provided under Microsoft Public License, which can be found at
//      http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx 
//
// @File: FormW3a.cs
//
// Purpose: Installation & Configuration Wizard - screen 3a (DB installation progress)
//
//
//
// Notes: 
//
//
//	
// History: 
//     @Version: V2.1
//
//     08/18/08 PGV 2.1.001.0 Starting V2.1 (based on 2.0.030.0 code)
//
// @EndHeader@
//*********************************************************************

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace H2V2.ConfigUtility
{
	public class FormW3 : H2V2.ConfigUtility.FormWbase
	{
		int m_Run = 0;

		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox tbStatement;
		private System.Windows.Forms.ProgressBar progressBar;
		private System.ComponentModel.IContainer components = null;

		public FormW3()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.tbStatement = new System.Windows.Forms.TextBox();
			this.progressBar = new System.Windows.Forms.ProgressBar();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.textBox1);
			this.panel1.Name = "panel1";
			// 
			// panel2
			// 
			this.panel2.Name = "panel2";
			// 
			// btCancel
			// 
			this.btCancel.Enabled = false;
			this.btCancel.Name = "btCancel";
			this.btCancel.Visible = false;
			// 
			// btNext
			// 
			this.btNext.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btNext.Enabled = false;
			this.btNext.Name = "btNext";
			// 
			// textBox1
			// 
			this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBox1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.textBox1.Location = new System.Drawing.Point(24, 16);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(560, 48);
			this.textBox1.TabIndex = 23;
			this.textBox1.TabStop = false;
			this.textBox1.Text = "Please wait while Wizard creates Repository Database";
			// 
			// tbStatement
			// 
			this.tbStatement.BackColor = System.Drawing.SystemColors.Control;
			this.tbStatement.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.tbStatement.Location = new System.Drawing.Point(40, 208);
			this.tbStatement.Name = "tbStatement";
			this.tbStatement.Size = new System.Drawing.Size(552, 13);
			this.tbStatement.TabIndex = 2;
			this.tbStatement.TabStop = false;
			this.tbStatement.Text = "";
			// 
			// progressBar
			// 
			this.progressBar.Location = new System.Drawing.Point(32, 232);
			this.progressBar.Maximum = 261;
			this.progressBar.Name = "progressBar";
			this.progressBar.Size = new System.Drawing.Size(560, 23);
			this.progressBar.Step = 1;
			this.progressBar.TabIndex = 3;
			// 
			// FormW3
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btNext;
			this.ClientSize = new System.Drawing.Size(640, 478);
			this.Controls.Add(this.progressBar);
			this.Controls.Add(this.tbStatement);
			this.Name = "FormW3a";
			this.Activated += new System.EventHandler(this.RunWrapper);
			this.Controls.SetChildIndex(this.tbStatement, 0);
			this.Controls.SetChildIndex(this.progressBar, 0);
			this.Controls.SetChildIndex(this.panel1, 0);
			this.Controls.SetChildIndex(this.panel2, 0);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
	
		void OnStatusChange (object source, StatusChangeEventArgs args)
		{
			if (args.m_IsException)
			{
				MessageBox.Show (args.m_exception.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
				MainForm.Dbi.Close ();
				this.DialogResult = DialogResult.Cancel;
				Close ();
			}
			else
			{
				tbStatement.Text = args.m_text;
				progressBar.PerformStep ();
			}

		}

		void RunWrapper (object sender, System.EventArgs e)
		{
			if (m_Run == 0)
			{
				m_Run++;

				Thread RunThread = new Thread (new ThreadStart (this.Run));
				RunThread.Start ();
			}
		}

		void Run ()
		{
			MainForm.Dbi.OnStatusChangeHandler += new DbInstaller.StatusChangeEventHandler(OnStatusChange);
			bool ret = MainForm.Dbi.InstallDb ();

			if (ret)
			{
				int w = MainForm.bWatson ? 1 : 0;
				MainForm.Dbi.SetWatson (w);
			}

			MainForm.Dbi.Close ();

			btNext.Enabled = true;
		}

	}
}

